package BandB.Tool.GPhoneManager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import android.content.Context;
import android.util.Log;

public final class ConfigManager {
	public static final int PIN_INDEX = 0;
	public static final int VERSION_INDEX = 1;
	public static final int PATH_INDEX = 2;
	public static final int GRID_INDEX = 3;
	public static final int AIRPLANE_INDEX = 4;
	public static final int BRIGHTNESS_INDEX = 5;
	public static final int SILENT_INDEX = 6;
	public static final int WIFI_INDEX = 7;
	public static final int FILE_INDEX = 8;
	public static final int APP_LIST_INDEX = 9;
	public static final int APP_GRID_INDEX = 10;
	public static final int TASK_LIST_INDEX = 11;
	public static final int TASK_GRID_INDEX = 12;
	private static final String CONFIG_FILE_NAME = "config.xml";
	private static final String TAG = ConfigManager.class.getSimpleName();
	private static final String ELEMENT_ROOT = "config";
	private static final String ELEMENT_ENABLE_PIN = "pin_to_noti_bar";
	private static final String ELEMENT_ENABLE_DIFFERENT_VERSION = "enable_multiply_backup";
	private static final String ELEMENT_BACKUP_PATH = "backup_path";
	private static final String ELEMENT_ENABLE_GRID_NOTI_BAR = "enable_grid_noti_bar";
	private static final String NODE_AIR = "airplane";
	private static final String NODE_BRIGHTNESS = "brightness";
	private static final String NODE_SILENT = "silent";
	private static final String NODE_WIFI = "wifi";
	private static final String MANAGER_FILE = "enable_file_manager";
	private static final String MANAGER_APP_LIST = "enable_app_list";
	private static final String MANAGER_APP_GRID = "enable_app_grid";
	private static final String MANAGER_TASK_LIST = "enable_task_list";
	private static final String MANAGER_TASK_GRID = "enable_task_grid";

	private static final boolean DEFAULT_PIN_ENABLED = false;
	private static final boolean DEFAULT_VERSION_ENABLED = true;
	private static final String DEFAULT_BACKUP_PATH = "BandBTool/";
	private static final boolean DEFAULT_GRID_ENABLED = false;
	private static final boolean DEFAULT_AIR = true;
	private static final boolean DEFAULT_BRIGHTNESS = true;
	private static final boolean DEFAULT_SILENT = true;
	private static final boolean DEFAULT_WIFI = true;
	private static final boolean DEFAULT_MANAGER = true;

	private static final int INFO_COUNT = 13;

	private Context mContext;
	private static ConfigManager instance;

	private ConfigManager(Context c) {
		mContext = c;
		File file = mContext.getFileStreamPath(CONFIG_FILE_NAME);
		if (file.exists()) {
			Log.i(TAG, "config file exist");
			// mDocument = new SAXReader().read(file);
		} else {
			Log.e(TAG, "file not exist, create file...");
			generateConfigFile();
		}
	}

	public static ConfigManager getInstance(Context c) {

		if (null == instance) {
			instance = new ConfigManager(c);
		}

		return instance;
	}

	private void generateConfigFile() {
		try {
			final FileOutputStream outPutStream = mContext.openFileOutput(
					CONFIG_FILE_NAME, Context.MODE_WORLD_WRITEABLE);
			outPutStream.close();
			final Document mDocument = DocumentHelper.createDocument();
			final Element rootElement = mDocument.addElement(ELEMENT_ROOT);
			final Element pinElement = rootElement
					.addElement(ELEMENT_ENABLE_PIN);
			pinElement.setText(String.valueOf(DEFAULT_PIN_ENABLED));
			final Element versionElement = rootElement
					.addElement(ELEMENT_ENABLE_DIFFERENT_VERSION);
			versionElement.setText(String.valueOf(DEFAULT_VERSION_ENABLED));
			final Element backupPathElement = rootElement
					.addElement(ELEMENT_BACKUP_PATH);
			backupPathElement.setText(DEFAULT_BACKUP_PATH);
			final Element gridElement = rootElement
					.addElement(ELEMENT_ENABLE_GRID_NOTI_BAR);
			rootElement.addElement(NODE_AIR).setText(
					String.valueOf(DEFAULT_AIR));
			rootElement.addElement(NODE_BRIGHTNESS).setText(
					String.valueOf(DEFAULT_BRIGHTNESS));
			rootElement.addElement(NODE_SILENT).setText(
					String.valueOf(DEFAULT_SILENT));
			rootElement.addElement(NODE_WIFI).setText(
					String.valueOf(DEFAULT_WIFI));

			rootElement.addElement(MANAGER_FILE).setText(
					String.valueOf(DEFAULT_MANAGER));
			rootElement.addElement(MANAGER_APP_LIST).setText(
					String.valueOf(DEFAULT_MANAGER));
			rootElement.addElement(MANAGER_APP_GRID).setText(
					String.valueOf(DEFAULT_MANAGER));
			rootElement.addElement(MANAGER_TASK_LIST).setText(
					String.valueOf(DEFAULT_MANAGER));
			rootElement.addElement(MANAGER_TASK_GRID).setText(
					String.valueOf(DEFAULT_MANAGER));

			gridElement.setText(String.valueOf(DEFAULT_GRID_ENABLED));
			final OutputFormat format = OutputFormat.createPrettyPrint();

			final XMLWriter output = new XMLWriter(
					new FileWriter(new File(mContext.getFilesDir()
							+ File.separator + CONFIG_FILE_NAME)), format);
			output.write(mDocument);
			output.close();
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public boolean getPinToNotiBarEnabled() {
		try {
			final Document document = new SAXReader().read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<?> nodes = rootElement.elements();
			final Element pinElement = (Element) nodes.get(PIN_INDEX);
			return Boolean.valueOf(pinElement.getText());
		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
			return false;
		}
	}

	public boolean getDifferentVersionEnabled() {
		try {
			final Document document = new SAXReader().read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<?> nodes = rootElement.elements();
			final Element versionElement = (Element) nodes.get(VERSION_INDEX);
			return Boolean.valueOf(versionElement.getText());
		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
			return false;
		}
	}

	public String getDefinedPath() {
		try {
			final Document document = new SAXReader().read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<?> nodes = rootElement.elements();
			final Element pathElement = (Element) nodes.get(PATH_INDEX);
			return pathElement.getText();
		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
			return DEFAULT_BACKUP_PATH;
		}
	}

	public boolean getGridNotiBarEnabled() {
		try {
			final Document document = new SAXReader().read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<?> nodes = rootElement.elements();
			final Element gridElement = (Element) nodes.get(GRID_INDEX);
			return Boolean.valueOf(gridElement.getText());
		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
			return DEFAULT_GRID_ENABLED;
		}
	}

	public boolean getAirEnabled() {
		try {
			final Document document = new SAXReader().read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<?> nodes = rootElement.elements();
			final Element gridElement = (Element) nodes.get(AIRPLANE_INDEX);
			return Boolean.valueOf(gridElement.getText());
		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
			return DEFAULT_AIR;
		}
	}

	public boolean getBrightnessEnabled() {
		try {
			final Document document = new SAXReader().read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<?> nodes = rootElement.elements();
			final Element gridElement = (Element) nodes.get(BRIGHTNESS_INDEX);
			return Boolean.valueOf(gridElement.getText());
		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
			return DEFAULT_BRIGHTNESS;
		}
	}

	public boolean getSilentEnabled() {
		try {
			final Document document = new SAXReader().read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<?> nodes = rootElement.elements();
			final Element gridElement = (Element) nodes.get(SILENT_INDEX);
			return Boolean.valueOf(gridElement.getText());
		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
			return DEFAULT_SILENT;
		}
	}

	public boolean getWifiEnabled() {
		try {
			final Document document = new SAXReader().read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<?> nodes = rootElement.elements();
			final Element gridElement = (Element) nodes.get(WIFI_INDEX);
			return Boolean.valueOf(gridElement.getText());
		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
			return DEFAULT_WIFI;
		}
	}

	public String[] getAllInfos() {
		String[] temp = new String[INFO_COUNT];
		try {
			final Document document = new SAXReader().read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<?> nodes = rootElement.elements();
			for (int i = 0; i < INFO_COUNT; i++) {
				temp[i] = ((Element) nodes.get(i)).getText();
			}
		} catch (final DocumentException e) {
			// TODO: handle exception
			Log.e(TAG, e.getMessage());
			return null;
		}

		return temp;
	}

	@SuppressWarnings("unchecked")
	public void setPinToNotiBarEnabled(final boolean bool) {
		try {
			final SAXReader reader = new SAXReader();
			final Document document = reader.read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<Element> nodeList = rootElement.elements();
			nodeList.get(PIN_INDEX).setText(String.valueOf(bool));
			final OutputFormat format = OutputFormat.createPrettyPrint();
			final XMLWriter output = new XMLWriter(
					new FileWriter(new File(mContext.getFilesDir()
							+ File.separator + CONFIG_FILE_NAME)), format);
			output.write(document);
			output.close();

		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public void setDifferentVersionEnabled(final boolean bool) {
		try {
			final SAXReader reader = new SAXReader();
			final Document document = reader.read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<Element> nodeList = rootElement.elements();
			nodeList.get(VERSION_INDEX).setText(String.valueOf(bool));
			final OutputFormat format = OutputFormat.createPrettyPrint();
			final XMLWriter output = new XMLWriter(
					new FileWriter(new File(mContext.getFilesDir()
							+ File.separator + CONFIG_FILE_NAME)), format);
			output.write(document);
			output.close();

		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public void setPath(final String path) {
		try {
			final SAXReader reader = new SAXReader();
			final Document document = reader.read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<Element> nodeList = rootElement.elements();
			nodeList.get(PATH_INDEX).setText(path);
			final OutputFormat format = OutputFormat.createPrettyPrint();
			final XMLWriter output = new XMLWriter(
					new FileWriter(new File(mContext.getFilesDir()
							+ File.separator + CONFIG_FILE_NAME)), format);
			output.write(document);
			output.close();
			Log.i(TAG, "write new config path:" + path);

		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public void setGridBarEnabled(final boolean bool) {
		try {
			final SAXReader reader = new SAXReader();
			final Document document = reader.read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<Element> nodeList = rootElement.elements();
			nodeList.get(GRID_INDEX).setText(String.valueOf(bool));
			final OutputFormat format = OutputFormat.createPrettyPrint();
			final XMLWriter output = new XMLWriter(
					new FileWriter(new File(mContext.getFilesDir()
							+ File.separator + CONFIG_FILE_NAME)), format);
			output.write(document);
			output.close();

		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public void setAirEnabled(final boolean bool) {
		try {
			final SAXReader reader = new SAXReader();
			final Document document = reader.read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<Element> nodeList = rootElement.elements();
			nodeList.get(4).setText(String.valueOf(bool));
			final OutputFormat format = OutputFormat.createPrettyPrint();
			final XMLWriter output = new XMLWriter(
					new FileWriter(new File(mContext.getFilesDir()
							+ File.separator + CONFIG_FILE_NAME)), format);
			output.write(document);
			output.close();

		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
		} catch (final IOException e) {
			// TODO: handle exception
			Log.e(TAG, e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	public void setBrightnessEnabled(final boolean bool) {
		try {
			final SAXReader reader = new SAXReader();
			final Document document = reader.read(new File(mContext
					.getFilesDir()
					+ File.separator + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<Element> nodes = rootElement.elements();
			nodes.get(5).setText(String.valueOf(bool));
			final OutputFormat format = OutputFormat.createPrettyPrint();
			final XMLWriter output = new XMLWriter(
					new FileWriter(new File(mContext.getFilesDir()
							+ File.separator + CONFIG_FILE_NAME)), format);
			output.write(document);
			output.close();

		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
		} catch (final IOException e) {
			// TODO: handle exception
			Log.e(TAG, e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	public void setSilentEnabled(final boolean bool) {
		try {
			final SAXReader reader = new SAXReader();
			final Document document = reader.read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<Element> nodeList = rootElement.elements();
			nodeList.get(6).setText(String.valueOf(bool));
			final OutputFormat format = OutputFormat.createPrettyPrint();
			final XMLWriter output = new XMLWriter(
					new FileWriter(new File(mContext.getFilesDir()
							+ File.separator + CONFIG_FILE_NAME)), format);
			output.write(document);
			output.close();

		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
		} catch (final IOException e) {
			// TODO: handle exception
			Log.e(TAG, e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	public void setWifiEnabled(final boolean bool) {
		try {
			final SAXReader reader = new SAXReader();
			final Document document = reader.read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<Element> nodeList = rootElement.elements();
			nodeList.get(WIFI_INDEX).setText(String.valueOf(bool));
			final OutputFormat format = OutputFormat.createPrettyPrint();
			final XMLWriter output = new XMLWriter(
					new FileWriter(new File(mContext.getFilesDir()
							+ File.separator + CONFIG_FILE_NAME)), format);
			output.write(document);
			output.close();

		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
		} catch (final IOException e) {
			// TODO: handle exception
			Log.e(TAG, e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	public void setFileManagerEnabled(final boolean bool) {
		try {
			final SAXReader reader = new SAXReader();
			final Document document = reader.read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<Element> nodeList = rootElement.elements();
			nodeList.get(FILE_INDEX).setText(String.valueOf(bool));
			final OutputFormat format = OutputFormat.createPrettyPrint();
			final XMLWriter output = new XMLWriter(
					new FileWriter(new File(mContext.getFilesDir()
							+ File.separator + CONFIG_FILE_NAME)), format);
			output.write(document);
			output.close();

		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
		} catch (IOException e) {
			// TODO: handle exception
			Log.e(TAG, e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	public void setAppListEnabled(final boolean bool) {
		try {
			final SAXReader reader = new SAXReader();
			final Document document = reader.read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<Element> nodeList = rootElement.elements();
			nodeList.get(APP_LIST_INDEX).setText(String.valueOf(bool));
			final OutputFormat format = OutputFormat.createPrettyPrint();
			final XMLWriter output = new XMLWriter(
					new FileWriter(new File(mContext.getFilesDir()
							+ File.separator + CONFIG_FILE_NAME)), format);
			output.write(document);
			output.close();

		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
		} catch (IOException e) {
			// TODO: handle exception
			Log.e(TAG, e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	public void setAppGridEnabled(final boolean bool) {
		try {
			final SAXReader reader = new SAXReader();
			final Document document = reader.read(new File(mContext
					.getFilesDir()
					+ File.separator + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<Element> nodeList = rootElement.elements();
			nodeList.get(APP_GRID_INDEX).setText(String.valueOf(bool));
			final OutputFormat format = OutputFormat.createPrettyPrint();
			final XMLWriter output = new XMLWriter(
					new FileWriter(new File(mContext.getFilesDir()
							+ File.separator + CONFIG_FILE_NAME)), format);
			output.write(document);
			output.close();

		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
		} catch (IOException e) {
			// TODO: handle exception
			Log.e(TAG, e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	public void setTaskListEnabled(final boolean bool) {
		try {
			final SAXReader reader = new SAXReader();
			final Document document = reader.read(new File(mContext
					.getFilesDir()
					+ File.separator + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<Element> nodeList = rootElement.elements();
			nodeList.get(TASK_LIST_INDEX).setText(String.valueOf(bool));
			final OutputFormat format = OutputFormat.createPrettyPrint();
			final XMLWriter output = new XMLWriter(
					new FileWriter(new File(mContext.getFilesDir()
							+ File.separator + CONFIG_FILE_NAME)), format);
			output.write(document);
			output.close();

		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
		} catch (IOException e) {
			// TODO: handle exception
			Log.e(TAG, e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	public void setTaskGridEnabled(final boolean bool) {
		try {
			final SAXReader reader = new SAXReader();
			final Document document = reader.read(new File(mContext
					.getFilesDir()
					+ "/" + CONFIG_FILE_NAME));
			final Element rootElement = document.getRootElement();
			final List<Element> nodeList = rootElement.elements();
			nodeList.get(TASK_GRID_INDEX).setText(String.valueOf(bool));
			final OutputFormat format = OutputFormat.createPrettyPrint();
			final XMLWriter output = new XMLWriter(
					new FileWriter(new File(mContext.getFilesDir()
							+ File.separator + CONFIG_FILE_NAME)), format);
			output.write(document);
			output.close();

		} catch (final DocumentException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, e.getMessage());
		} catch (final IOException e) {
			// TODO: handle exception
			Log.e(TAG, e.getMessage());
		}
	}
}
